package cn.itcast.controller.cargo;

import cn.itcast.controller.BaseController;
import cn.itcast.domain.cargo.*;
import cn.itcast.service.cargo.ContractProductService;
import cn.itcast.service.cargo.ExtCproductService;
import cn.itcast.service.cargo.FactoryService;
import cn.itcast.utils.FileUploadUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/cargo/extCproduct")
public class ExtCproductController extends BaseController {

    @Reference
    private ContractProductService contractProductService;
    @Reference
    private FactoryService factoryService;
    @Reference
    private ExtCproductService extCproductService;

    @RequestMapping(value = "/list",name = "展示货物下附件列表数据")
    public String findAll(String contractId, String contractProductId,
                          @RequestParam(value ="page",defaultValue = "1") Integer pageNumber,
                          @RequestParam(value ="size",defaultValue = "10")  Integer pageSize){


//        查询货物下附件数据  select * from co_ext_cproduct where contract_product_id=?
        ExtCproductExample example = new ExtCproductExample();
        example.createCriteria().andContractProductIdEqualTo(contractProductId);
        PageInfo pageInfo = extCproductService.findAll(example, pageNumber, pageSize);
        request.setAttribute("page",pageInfo);
//        查询生产附件的工厂 select * from co_factory where ctype='附件'
        FactoryExample factoryExample = new FactoryExample();
        factoryExample.createCriteria().andCtypeEqualTo("附件");
        List<Factory> factoryList = factoryService.findAll(factoryExample);
        request.setAttribute("factoryList",factoryList);

//        当前合同的id
        request.setAttribute("contractId",contractId);
//        当前货物的id
        request.setAttribute("contractProductId",contractProductId);

        return "cargo/extc/extc-list";
    }


    @RequestMapping(value = "/toAdd",name = "进入到新增购销合同页面")
    public String toAdd(){

        return "cargo/contract/contract-add";
    }


    @RequestMapping(value = "/toUpdate",name = "进入到货物下附件页面")
    public String toUpdate(String id,String contractId,String contractProductId){  // 货物id
        ExtCproduct extCproduct = extCproductService.findById(id);
        request.setAttribute("extCproduct",extCproduct);

        FactoryExample factoryExample = new FactoryExample();
        factoryExample.createCriteria().andCtypeEqualTo("附件");
        List<Factory> factoryList = factoryService.findAll(factoryExample);
        request.setAttribute("factoryList",factoryList);

        request.setAttribute("contractProductId",contractProductId);
        request.setAttribute("contractId",contractId);

        return "cargo/extc/extc-update";
    }

    @Autowired
    private FileUploadUtil fileUploadUtil;

    @RequestMapping(value = "/edit" ,name = "保存货物下附件数据")
    public String edit(ExtCproduct extCproduct, MultipartFile productPhoto ){
//        把图片上传到七牛云
        String productImage = null;
        try {
            productImage = fileUploadUtil.upload(productPhoto);
            extCproduct.setProductImage(productImage);
        } catch (Exception e) {

        }

        if(StringUtils.isEmpty(extCproduct.getId())){
            extCproduct.setId(UUID.randomUUID().toString());
            extCproduct.setCompanyId(getCompanyId());
            extCproduct.setCompanyName(getCompanyName());
            extCproductService.save(extCproduct);
        }else {
            extCproductService.update(extCproduct);
        }
        return "redirect:/cargo/extCproduct/list.do?contractId="+extCproduct.getContractId()+"&contractProductId="+extCproduct.getContractProductId();
    }


    @RequestMapping(value = "/delete" ,name = "删除货物下附件数据")
    public String delete(String id,String contractId,String contractProductId){
        extCproductService.delete(id);
        return "redirect:/cargo/extCproduct/list.do?contractId="+contractId+"&contractProductId="+contractProductId;
    }




}
